package com.wu.gumomallproduct.product.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.wu.common.entity.product.SkuImagesEntity;
import com.wu.common.entity.product.SpuInfoDescEntity;
import com.wu.common.vo.product.SkuItemSaleAttrVo;
import com.wu.common.vo.product.SkuItemVo;
import com.wu.common.vo.product.SpuItemAttrGroupVo;
import com.wu.gumomallproduct.product.service.SkuImagesService;
import com.wu.gumomallproduct.product.service.SpuInfoDescService;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.concurrent.CompletableFuture;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.wu.common.utils.PageUtils;
import com.wu.common.utils.Query;

import com.wu.gumomallproduct.product.dao.SkuInfoDao;
import com.wu.common.entity.product.SkuInfoEntity;
import com.wu.gumomallproduct.product.service.SkuInfoService;


@Service("skuInfoService")
public class SkuInfoServiceImpl extends ServiceImpl<SkuInfoDao, SkuInfoEntity> implements SkuInfoService {

    @Autowired
    private ThreadPoolTaskExecutor taskExecutor;

    @Autowired
    private SkuImagesService skuImagesService;
    @Autowired
    private SpuInfoDescService spuInfoDescService;

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        QueryWrapper<SkuInfoEntity> queryWrapper = new QueryWrapper<>();

        String key = (String) params.get("key");
        if (!StringUtils.isEmpty(key) && !"0".equalsIgnoreCase(key)) {
            queryWrapper.and((wrapper) -> {
                wrapper.eq("sku_id",key).or().like("sku_name",key);
            });
        }

        String catelogId = (String) params.get("catelogId");
        if (!StringUtils.isEmpty(catelogId) && !"0".equalsIgnoreCase(catelogId)) {
            queryWrapper.eq("catalog_id",catelogId);
        }

        String brandId = (String) params.get("brandId");
        if (!StringUtils.isEmpty(brandId) && !"0".equalsIgnoreCase(brandId)) {
            queryWrapper.eq("brand_id",brandId);
        }

        String min = (String) params.get("min");
        if (!StringUtils.isEmpty(min)) {
            queryWrapper.ge("price",min);
        }

        String max = (String) params.get("max");

        if (!StringUtils.isEmpty(max)) {
            try {
                BigDecimal bigDecimal = new BigDecimal(max);
                if (bigDecimal.compareTo(BigDecimal.ZERO) == 1) {
                    queryWrapper.le("price",max);
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
        }


        IPage<SkuInfoEntity> page = this.page(
                new Query<SkuInfoEntity>().getPage(params),
                queryWrapper
        );
        return new PageUtils(page);
    }

    @Override
    public SkuItemVo item(Long skuId) {
        SkuItemVo skuItemVo = new SkuItemVo();
        //1、sku基本信息的获取  pms_sku_info
        CompletableFuture<SkuInfoEntity> skuInfoFuture = CompletableFuture.supplyAsync(() -> {
            SkuInfoEntity skuInfoEntity = this.getById(skuId);
            log.warn("sku基本信息的获取："+skuInfoEntity);
            skuItemVo.setInfo(skuInfoEntity);
            return skuInfoEntity;
        }, taskExecutor);

        //2、sku的图片信息    pms_sku_images
        CompletableFuture<Void> skuImgFuture = CompletableFuture.runAsync(() -> {
            List<SkuImagesEntity> images = skuImagesService.list(new LambdaQueryWrapper<SkuImagesEntity>().eq(SkuImagesEntity::getSkuId, skuId));
            log.warn("sku的图片信息"+images);
            skuItemVo.setImages(images);
        }, taskExecutor);
        //3、获取spu的销售属性组合
        CompletableFuture<Void> saleAttrFuture = skuInfoFuture.thenAcceptAsync(skuInfoEntity -> {
            List<SkuItemSaleAttrVo> saleAttrs = baseMapper.getSpuSaleAttrGroupVo(skuInfoEntity.getSpuId());
            log.warn("获取spu的销售属性组合"+saleAttrs);
            skuItemVo.setSaleAttr(saleAttrs);
        }, taskExecutor);
        //4、获取spu的介绍    pms_spu_info_desc
        CompletableFuture<Void> descFuture = skuInfoFuture.thenAcceptAsync(skuInfoEntity -> {
            SpuInfoDescEntity desc = spuInfoDescService.getById(skuInfoEntity.getSpuId());
            log.warn("获取spu的介绍"+desc);
            skuItemVo.setDesc(desc);
        }, taskExecutor);
        //5、获取spu的规格参数信息
        CompletableFuture<Void> groupAttrsFuture = skuInfoFuture.thenAcceptAsync(skuInfoEntity -> {
            List<SpuItemAttrGroupVo> groupAttrs = baseMapper.getSpuItemAttrGroupVo(skuInfoEntity.getSpuId(), skuInfoEntity.getCatalogId());
            log.warn("获取spu的规格参数信息"+groupAttrs);
            skuItemVo.setGroupAttrs(groupAttrs);
        }, taskExecutor);

        CompletableFuture.allOf(skuInfoFuture,skuImgFuture,saleAttrFuture,descFuture,groupAttrsFuture);


        return skuItemVo;
    }

}